package com.octant.testipc;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.tuya.smart.home.sdk.TuyaHomeSdk;
import com.tuya.smart.sdk.api.IDevListener;
import com.tuya.smart.sdk.api.ITuyaDevice;
import com.tuya.smart.sdk.bean.DeviceBean;

import androidx.appcompat.app.AppCompatActivity;

public class DeviceSdcardActivity extends AppCompatActivity implements View.OnClickListener  {

    private Context mContext;
    private String devId;
    private DeviceBean deviceBean;
    private ITuyaDevice mDevice;
    private TextView tv_sdcard_storage;
    private Button btn_format,btn_record_settings;
    private Switch switch_auto_record;
    private Spinner spn_record_mode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device_sdcard);
        mContext = DeviceSdcardActivity.this;
        devId = getIntent().getStringExtra("devId");
        deviceBean = TuyaHomeSdk.getDataInstance().getDeviceBean(devId);
        mDevice = TuyaHomeSdk.newDeviceInstance(deviceBean.getDevId());
        initViews();
    }

    private void initViews() {
        tv_sdcard_storage = findViewById(R.id.tv_sdcard_storage);
        btn_format = findViewById(R.id.btn_format);
        switch_auto_record = findViewById(R.id.switch_auto_record);
        spn_record_mode = findViewById(R.id.spn_record_mode);
        btn_record_settings = findViewById(R.id.btn_record_settings);

        btn_format.setOnClickListener(this);
        btn_record_settings.setOnClickListener(this);

    }

    @Override
    protected void onResume() {
        super.onResume();
        mDevice.registerDevListener(devListener);
        Object sdcardValue = DpUtil.queryValueByDPID(devId,DpUtil.DP_ID_SDCARD_STATUS);
        setViewsFromSdcardStatus(sdcardValue);
        Object spaceValue = DpUtil.queryValueByDPID(devId,DpUtil.DP_ID_SDCARD_STORAGE_SPACE);
        setViewsFromStorageSpace(spaceValue);
        Object recordSwitchValue = DpUtil.queryValueByDPID(devId,DpUtil.DP_ID_SDCARD_RECORD_SWITCH);
        setViewsFromRecordSwitch(recordSwitchValue);
        Object recordModeValue = DpUtil.queryValueByDPID(devId,DpUtil.DP_ID_SDCARD_RECORD_MODE);
        setViewsFromRecordMode(recordModeValue);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mDevice.unRegisterDevListener();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mDevice.onDestroy();
    }

    private void setViewsFromSdcardStatus(Object value){
        if (value != null){
            String sdcard_status = String.valueOf(value);
            if (sdcard_status.equals("1")){
                //正常
                btn_format.setVisibility(View.VISIBLE);
            }else if (sdcard_status.equals("2")){
                //SD 卡损坏或格式不对
                tv_sdcard_storage.setText("SD卡损坏或格式不对");
                btn_format.setVisibility(View.VISIBLE);
            }else if (sdcard_status.equals("3")){
                //空间不足
                tv_sdcard_storage.setText("SD卡空间不足");
                btn_format.setVisibility(View.VISIBLE);
            }else if (sdcard_status.equals("4")){
                //正在格式化
                tv_sdcard_storage.setText("SD卡正在格式化");
                btn_format.setVisibility(View.GONE);
            }else if (sdcard_status.equals("5")){
                //无 SD 卡
                tv_sdcard_storage.setText("没检测到SD卡");
                btn_format.setVisibility(View.GONE);
            }
        }
    }
    private void setViewsFromFormatStatus(Object value){
        if (value != null){
            String format_status = String.valueOf(value);
            tv_sdcard_storage.setText("格式化进度："+format_status);
        }
    }

    private void setViewsFromStorageSpace(Object value){
        if (value != null){
            String space = String.valueOf(value);
            tv_sdcard_storage.setText(space+"(KB)");
        }
    }

    private void setViewsFromRecordSwitch(Object value){
        if (value != null){
            String switchStr = String.valueOf(value);
            if (switchStr.equals("true")){
                switch_auto_record.setChecked(true);
            }else {
                switch_auto_record.setChecked(false);
            }
        }
    }

    private void setViewsFromRecordMode(Object value){
        if (value != null){
            String mode = String.valueOf(value);
            if (mode.equals("1")){
                spn_record_mode.setSelection(0);
            }else {
                spn_record_mode.setSelection(1);
            }
        }
    }


    IDevListener devListener = new IDevListener() {

        /**
         * DP 数据更新
         *
         * @param devId 设备 ID
         * @param dpStr 设备发生变动的功能点，为 JSON 字符串，数据格式：{"101": true}
         */
        @Override
        public void onDpUpdate(String devId, String dpStr) {
            Object sdcardValue = DpUtil.queryValueByKey(dpStr,DpUtil.DP_ID_SDCARD_STATUS);
            setViewsFromSdcardStatus(sdcardValue);
            Object spaceValue = DpUtil.queryValueByKey(dpStr,DpUtil.DP_ID_SDCARD_STORAGE_SPACE);
            setViewsFromStorageSpace(spaceValue);
            Object formatStatusValue = DpUtil.queryValueByKey(dpStr,DpUtil.DP_ID_SDCARD_FORMAT_STATUS);
            setViewsFromFormatStatus(formatStatusValue);
            Object recordSwitchValue = DpUtil.queryValueByKey(dpStr,DpUtil.DP_ID_SDCARD_RECORD_SWITCH);
            setViewsFromRecordSwitch(recordSwitchValue);
            Object recordModeValue = DpUtil.queryValueByKey(dpStr,DpUtil.DP_ID_SDCARD_RECORD_MODE);
            setViewsFromRecordMode(recordModeValue);
        }

        /**
         * 设备移除回调

         *
         * @param devId 设备id

         */
        @Override
        public void onRemoved(String devId){
            Toast.makeText(mContext, "设备已经被移除", Toast.LENGTH_SHORT).show();
            finish();
        }

        /**
         * 设备上下线回调。如果设备断电或断网，服务端将会在3分钟后回调到此方法。

         *
         * @param devId  设备 ID
         * @param online 是否在线，在线为 true

         */
        @Override
        public void onStatusChanged(String devId, boolean online){
        }

        /**
         * 网络状态发生变动时的回调

         *
         * @param devId  设备 ID
         *  @param status 网络状态是否可用，可用为 true

         */
        @Override
        public void onNetworkStatusChanged(String devId, boolean status){
        }

        /**
         * 设备信息更新回调

         *
         * @param devId  设备 ID

         */
        @Override
        public void onDevInfoUpdate(String devId){
        }

    };

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_format:{
                DpUtil.publishDps(mContext,mDevice,DpUtil.DP_ID_SDCARD_FORMAT_COMMAND,true);
            }break;
            case R.id.btn_record_settings:{
                boolean recordSwitch = switch_auto_record.isChecked();
                DpUtil.publishDps(mContext,mDevice,DpUtil.DP_ID_SDCARD_RECORD_SWITCH,recordSwitch);
                if (recordSwitch){
                    int position = spn_record_mode.getSelectedItemPosition();
                    DpUtil.publishDps(mContext,mDevice,DpUtil.DP_ID_SDCARD_RECORD_MODE,position+1);
                }
            }break;
        }
    }
}
